﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Microsoft.Telco.Controls.WindowsPhone
{
    /// <summary>
    /// Country flag selector.
    /// </summary>
    public partial class CountryPicker : UserControl
    {
        #region events
        /// <summary>
        /// Raised when country selection changes. Payload is country code.
        /// </summary>
        public event Action<string> SelectedCountryChanged;
        #endregion

        #region properties
        ObservableCollection<Country> countries;
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="CountryPicker"/> class.
        /// </summary>
        public CountryPicker()
        {
            // Required to initialize variables
            InitializeComponent();
            if (!ControlHelper.PopupTracker.ContainsKey("popupCountries"))
            {
                ControlHelper.PopupTracker.Add("popupCountries", popupCountries);
            }
            Loaded += CountryPicker_Loaded;
        }

        #region event handlers
        /// <summary>
        /// Selects the flag from country code.
        /// </summary>
        /// <param name="code">The code.</param>
        public void SelectFlagFromCountryCode(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {

            }
            else
            {
                //countryImage.Source = new BitmapImage(new Uri("images/Unknown.png", UriKind.Relative));
            }
        }

        private void CountryPicker_Loaded(object sender, RoutedEventArgs e)
        {
            countries = CountryHelper.GetCountries();
            listCountries.ItemsSource = countries;
            foreach (var country in countries)
            {
                //TODO: decide default country based on localization
                if (country.ShortName == "US")
                {
                    countryImage.Source = new BitmapImage(new Uri(country.ImageUrl, UriKind.Relative));
                    break;
                }
            }
        }

        private void bdrCountryPicker_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            bool shouldOpen = !popupCountries.IsOpen;
            if (shouldOpen)
            {
                popupCountries.HorizontalOffset = 0; //CountryHelper.GetAbsolutePosition(bdrCountryPicker, Corner.LeftBottom).X;
                popupCountries.VerticalOffset = 30; //CountryHelper.GetAbsolutePosition(bdrCountryPicker, Corner.LeftBottom).Y;
            }
            popupCountries.IsOpen = shouldOpen;
            e.Handled = true;
        }

        private void countryImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            //countryImage.Source = new BitmapImage(new Uri("images/Unknown.png", UriKind.Relative));
        }

        private void Grid_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            popupCountries.IsOpen = false;
            var selectedCountry = (sender as Grid).DataContext as Country;
            if (selectedCountry != null)
            {
                if (SelectedCountryChanged != null)
                {
                    SelectedCountryChanged(selectedCountry.Code);
                }
                countryImage.Source = new BitmapImage(new Uri(selectedCountry.ImageUrl, UriKind.Relative));
            }
        }
        #endregion
    }
}